export default class filter {
    // 判断字符串是否为空
  isEmpty (str) {
    return str == '' || str == null || str == 'null'
  }
    // 判断字符串是否不为空
  isNotEmpty (str) {
    return !this.isEmpty(str)
  }
    // 浮点求和
  sum (numbers, toFixed = 2) {
    let sum = 0
    for (const str of numbers) {
      if (!this.isNumber(str)) {
        return NaN
      }
      const num = parseFloat(str)
      if (isNaN(num)) {
        return NaN
      }
      sum += num
    }
    return sum.toFixed(toFixed)
  }
    // 数字判断
  isNumber (value) {
    const patrn = /^[-+]?\d+(\.\d+)?$/
    return patrn.test(value)
  }

    // 数字判断
  isPositiveNumber (value) {
    const patrn = /^[1-9]\d*$|^\.\d*$|^0\.\d*$|^[1-9]\d*\.\d*$|^0$/
    return patrn.test(value)
  }
    // 数组判断
  isArray (o) {
    return Object.prototype.toString.call(o) === '[object Array]'
  }
    // 事件转日期
  convertTimestapeToDay (timestape) {
    return timestape.substring(0, timestape.indexOf(' ')).replace(/-/g, '.')
  }

    // 格式化时间
    /**
     * function: 格式化时间
     * 0-60分钟显示“XX分钟前“
     * 1-24小时显示”XX小时前“
     * 超过1天显示”XX天前“
     * @param {*} time
     */
  timeFromat (time) {
        //  typeof time
    time = time.replace(/\-/g, '/')
    let remoteTime = new Date(time).getTime(),
      nowTime = new Date().getTime(),
      second = (nowTime - remoteTime) / 1000,
      minute = (nowTime - remoteTime) / (1000 * 60),
      hourse = minute / 60,
      days = hourse / 24,
      years = days / 365

    if (second > 0 && second < 60) {
      return Math.floor(second) + '秒前'
    } else if (minute > 0 && minute < 60) {
      return Math.floor(minute) + '分钟前'
    } else if (hourse >= 1 && hourse <= 24) {
      return Math.floor(hourse) + '小时前'
    } else if (days > 1 && days < 365) {
      return Math.floor(days) + '天前'
    } else if (years > 1) {
      return Math.floor(days) + '年前'
    } else if (Math.abs(second) !== 0) {
      return Math.floor(Math.random() * 60) + '分钟前'
    }
  }
    // 格式化日期
  dateFormate (date, fmt) {
    const o = {
      'M+': date.getMonth() + 1,
      'd+': date.getDate(),
      'h+': date.getHours(),
      'm+': date.getMinutes(),
      's+': date.getSeconds(),
      'q+': Math.floor((date.getMonth() + 3) / 3),
      'S': date.getMilliseconds()
    }
    if (/(y+)/.test(fmt)) fmt = fmt.replace(RegExp.$1, (date.getFullYear() + '').substr(4 - RegExp.$1.length))
    for (let k in o) {
      if (new RegExp('(' + k + ')').test(fmt)) fmt = fmt.replace(RegExp.$1, (RegExp.$1.length == 1) ? (o[k]) : (('00' + o[k]).substr(('' + o[k]).length)))
    }
    return fmt
  }
    // 2018-01-20 => 2018.01.20
  static convertTimestapeToDay (timestape) {
    return timestape.substring(0, timestape.indexOf(' ')).replace(/-/g, '.')
  }

    // 格式化时间
    /**
     * function: 格式化时间
     * 0-60分钟显示“XX分钟前“
     * 1-24小时显示”XX小时前“
     * 超过1天显示”XX天前“
     * @param {*} time
     */
  static timeFromat (time) {
        //  typeof time
    time = time.replace(/\-/g, '/')
    let remoteTime = new Date(time).getTime(),
      nowTime = new Date().getTime(),
      second = (nowTime - remoteTime) / 1000,
      minute = (nowTime - remoteTime) / (1000 * 60),
      hourse = minute / 60,
      days = hourse / 24,
      years = days / 365

    if (second > 0 && second < 60) {
      return Math.floor(second) + '秒前'
    } else if (minute > 0 && minute < 60) {
      return Math.floor(minute) + '分钟前'
    } else if (hourse >= 1 && hourse <= 24) {
      return Math.floor(hourse) + '小时前'
    } else if (days > 1 && days < 365) {
      return Math.floor(days) + '天前'
    } else if (years > 1) {
      return Math.floor(days) + '年前'
    } else if (Math.abs(second) !== 0) {
      return Math.floor(Math.random() * 60) + '分钟前'
    }
  }
    // 格式化日期
  static dateFormate (date, fmt) {
    const o = {
      'M+': date.getMonth() + 1,
      'd+': date.getDate(),
      'h+': date.getHours(),
      'm+': date.getMinutes(),
      's+': date.getSeconds(),
      'q+': Math.floor((date.getMonth() + 3) / 3),
      'S': date.getMilliseconds()
    }
    if (/(y+)/.test(fmt)) fmt = fmt.replace(RegExp.$1, (date.getFullYear() + '').substr(4 - RegExp.$1.length))
    for (let k in o) {
      if (new RegExp('(' + k + ')').test(fmt)) fmt = fmt.replace(RegExp.$1, (RegExp.$1.length == 1) ? (o[k]) : (('00' + o[k]).substr(('' + o[k]).length)))
    }
    return fmt
  }
}
